package com.leetcode.LC_MED;

import java.util.ArrayList;
import java.util.List;

public class LC129_2 {
    public int sumNumbers(TreeNode root) {

        ArrayList<Integer> list = new ArrayList<>();
        dfs(root, list, 0);
        int sum = 0;
        for (Integer integer : list) {
            sum += integer;
        }
        return sum;

    }

    private void dfs(TreeNode root, List<Integer> list, int sum) {
        //sum为各个路径的数字
        if (root == null) {
            return;
        }
        sum=sum*10+root.val;
        if (root.right == null && root.left == null) {
            list.add(Integer.valueOf(sum));
        }

        dfs(root.left, list, sum);
        dfs(root.right, list, sum);
        sum=(sum-root.val)/10;
    }
}
